package day12;

import java.util.Scanner;

public class A3 {
    public static void main(String[] args) {
        try {
            System.out.println("正在运行程序");
            throw new MyException1("发生了MyException1异常");
        }
        catch (MyException1 e) {
            System.err.println(e.getMessage());
        }
        try {
            System.out.println("正在运行程序");
            throw new MyException2("发生了MyException2异常");
        }
        catch (MyException2 e) {
            System.err.println(e.getMessage());
        }
    }
}

class MyException1 extends Exception {
    public MyException1() {
        System.out.println("发生了MyException1异常");
    }
    public MyException1(String msg) {
        super(msg);
    }
}

class MyException2 extends RuntimeException {
    public MyException2() {
        System.out.println("发生了MyException2异常");
    }
    public MyException2(String msg) {
        super(msg);
    }
}

class Bank{
    static double balance;
    public Bank(double addition) {
        balance += addition;
    }
    public void WithDrawal(double damount) {
        if (damount<0) {
            try {
                throw new NagativeException("取款数额为负");
            }
            catch (NagativeException e) {
                System.err.println(e.getMessage());
            }
        }
        else if (balance >= damount) {
            balance -= damount;
            System.out.println("取款成功");
        }
        else if (balance < damount) {
            try {
                throw new BlanceException("余额不足");
            }
            catch (BlanceException e){
                System.err.println(e.getMessage());
            }
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入初始金额:");
        Double balance = sc.nextDouble();
        Bank bank = new Bank(balance);
        while (true) {
            System.out.println("请输入需要的功能: 1.存款  2.取款 其他:结束服务");
            switch(sc.nextInt()) {
                case 1:
                    System.out.println("请输入存款的金额:");
                    new Bank(sc.nextDouble());
                    break;
                case 2:
                    System.out.println("请输入取款的金额:");
                    bank.WithDrawal(sc.nextDouble());
                    break;
                case 3:System.out.println("结束服务");System.exit(0);
            }
        }
    }
}

class BlanceException extends RuntimeException {
    public BlanceException(String msg) {
        super(msg);
    }
}

class NagativeException extends RuntimeException {
    public NagativeException(String msg) {
        super(msg);
    }
}

class A4 {
    public static void main(String[] args) {
        int n = 10;
        int[][] triangle = new int[n][n];
        for (int i = 0; i < n; i++) {
            triangle[i][0] = 1;
            for (int j = 1; j <= i; j++) {
                triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i; j++) {
                System.out.print("#");
            }
            for (int j = 0; j <= i; j++) {
                System.out.printf("%4d", triangle[i][j]);
            }
            System.out.println();
        }
    }
}

class B4{
    static double weight=0;
    static double addition=0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("是否继续转载集装箱？1.继续 2.停止");
            int choose = sc.nextInt();
            if (choose == 1) {
                System.out.println("请输入装载量:");
                addition = sc.nextDouble();
                weight = addition + weight;
                System.out.println("目前装载了" + weight);
                if (addition + weight > 1000) {
                    try {
                        throw new OverweightException("无法装载重量是" + addition + "吨的货物");
                    } catch (OverweightException e) {
                        System.err.println(e.getMessage());
                        System.out.println("货船准时启航");
                        System.exit(0);
                    }
                }
            }
            else if (choose == 2) {
                try {
                    throw new OverweightException("停止装船");
                } catch (OverweightException e) {
                    System.err.println(e.getMessage());
                    System.out.println("货船准时启航");
                    System.exit(0);
                }
            }
        }
    }
}

class OverweightException extends RuntimeException {
    public OverweightException(String msg) {
        super(msg);
    }
}